# Table 3
# libs --------------------------------------------------------------------
library(here)
library(tidyverse)
library(haven)
library(lfe)
library(xtable)
library(latex2exp)

# data --------------------------------------------------------------------
data <- read_dta(file = '../data/modified data/analysis.dta')

# make table 3 col 1
m1 <- felm(turnout_share ~ treat | county_id + state_year_id | 0 | county_id, data = data)
b1 <- round(m1$coefficients[1], 3)
se1 <- round(m1$cse[1], 3)
n1 <- m1$N
nc1 <- length(levels(m1$fe$county_id))
ne1 <- length(levels(m1$fe$state_year_id))

# make table 3 col 2
temp <- data %>% mutate(county_id = as.factor(county_id), year = as.numeric(year))
m2 <- felm(turnout_share ~ treat | county_id + county_id:year + state_year_id | 0 | county_id, data = temp)
b2 <- round(m2$coefficients[1], 3)
se2 <- round(m2$cse[1], 3)
n2 <- m2$N
nc2 <- length(levels(m2$fe[["year:county_id"]]))
ne2 <- length(levels(m2$fe$state_year_id))

# make table 3 col 3
temp <- data %>% mutate(county_id = as.factor(county_id), year = as.numeric(year), year2 = as.numeric(year2))
m3 <- felm(turnout_share ~ treat | county_id:year2 + county_id:year + state_year_id | 0 | county_id, data = temp)
b3 <- round(m3$coefficients[1], 3)
se3 <- round(m3$cse[1], 3)
n3 <- m3$N
nc3 <- length(levels(m3$fe[["year:county_id"]]))
ne3 <- length(levels(m3$fe$state_year_id))

# create dataset with only CA for VBM share
temp <- data %>% mutate(county_id = as.factor(county_id), year = as.numeric(year), year2 = as.numeric(year2)) %>% 
  filter(state == "CA")

# make table 3 col 4
m4 <- felm(vbm_share ~ treat | county_id + state_year_id | 0 | county_id, data = temp)
b4 <- round(m4$coefficients[1], 3)
se4 <- round(m4$cse[1], 3)
n4 <- m4$N
nc4 <- length(levels(m4$fe$county_id))
ne4 <- length(levels(m4$fe$state_year_id))

# make table 3 col 5
m5 <- felm(vbm_share ~ treat | county_id + state_year_id + county_id:year| 0 | county_id, data = temp)
b5 <- round(m5$coefficients[1], 3)
se5 <- round(m5$cse[1], 3)
n5 <- m5$N
nc5 <- length(levels(m5$fe$county_id))
ne5 <- length(levels(m5$fe$state_year_id))

# make table 3 col 6
m6 <- felm(vbm_share ~ treat | state_year_id + county_id + county_id:year + county_id:year2 | 0 | county_id, data = temp)
b6 <- round(m6$coefficients[1], 3)
se6 <- round(m6$cse[1], 3)
n6 <- m6$N
nc6 <- length(levels(m6$fe$county_id))
ne6 <- length(levels(m6$fe$state_year_id))

# build table
tbl <- tibble(" " = c("VBM", "", "# Counties", "# Elections","# Obs", "County FE", "State by Year FE", "County Trends"),
              "(1)" = c(b1, paste0("(", se1, ")"), nc1, ne1, n1, "Yes", "Yes", "No"),
              "(2)" = c(b2, paste0("(", se2, ")"), nc2, ne2, n2, "Yes", "Yes", "Linear"),
              "(3)" = c(b3, paste0("(", se3, ")"), nc3, ne3, n3, "Yes", "Yes", "Quad"),
              "(4)" = c(b4, paste0("(", se4, ")"), nc4, ne4, n4, "Yes", "Yes", "No"),
              "(5)" = c(b5, paste0("(", se5, ")"), nc5, ne5, n5, "Yes", "Yes", "Linear"),
              "(6)" = c(b6, paste0("(", se6, ")"), nc6, ne6, n6, "Yes", "Yes", "Quad"))
x <- xtable(tbl, caption = "\\textbf{Vote-byMail Expansion Increases Participation.}", label = "tab:participation")
align(x) <- c("l", "l", rep("c", 6))
print(x, file = "../repfig/participation_table.tex", compress = F, hline.after = c(-1, -1, 0, 5),
      include.rownames = F, include.colnames = F, caption.placement = "top",
      add.to.row = list(pos = list(0, 0, nrow(tbl)), command = c("& \\multicolumn{3}{c}{Dem Turnout Share [0-1]} & \\multicolumn{3}{c}{Dem Vote Share [0-1]} \\\\\n",
                                                      "& (1) & (2) & (3) & (4) & (5) & (6) \\\\\n",
                                                      "\\hline\\hline\\multicolumn{7}{p{.8\\textwidth}}{\\footnotesize{Robust standard errors clustered by county in parentheses.}} \\\\\n")))
